﻿using Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class AddressService
    {
        /// <summary>
        /// 根据客户id获取收货地址数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static List<Address> GetAddresses(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Address.Where(p => p.Custid == Id).ToList();
            }
        }
        /// <summary>
        /// 获取地址信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<Address> GetAddress(int limit, int page, string Name, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.Address.Where(p => true);
                if (!string.IsNullOrEmpty(Name))
                {
                    query = query.Where(p => p.Nam.Contains(Name));
                }
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }

        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <returns></returns>
        public static int Addizhi(Address address)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                db.Address.Add(address);

                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 根据地址id进行修改数据
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public static int UpdateDiZhi(Address address, int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Address address1 = db.Address.FirstOrDefault(p => p.Id == id);

                db.Address.AddOrUpdate(address);

                return db.SaveChanges();
            }
        }


        /// <summary>
        /// 根据地址id获取数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Address GetAddressId(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Address.FirstOrDefault(p => p.Id == Id);
            }
        }
        /// <summary>
        /// 根据地址id修改默认地址
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static int UpdateMoRen(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Address address1 = db.Address.FirstOrDefault(p => p.Id == Id);
                address1.IsDefault = "是";
                return db.SaveChanges();
            }
        }

        public static int Delete(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Address address1 = new Address() { Id = Id };
               
                db.Address.Attach(address1);
                db.Address.Remove(address1);
                return db.SaveChanges();
            }
        }
    }
}
